let image = $('#image');

let option = {
    aspectRatio: 1,
    preview: '.img-preview'
};
image.cropper(option);


$('button:contains("上传")').on('click', function () {
    $('#file').trigger('click');
})


$('#file').on('change', function () {
    if (this.files.length > 0) {
        let fileObj = this.files[0];
        let url = URL.createObjectURL(fileObj);
        image.cropper('replace', url);
    }
})

$('button:contains("上传")').on('click', function () {
    let canvas = image.cropper('getCroppedCanvas', { width: 30, height: 30 });
    let str = canvas.toDataURL('image/jpeg',0.7);

    $.ajax({
        type: 'POST',
        url: '/my/user/avatar',
        data: { avatar: str },
        success: function (res) {
            layer.msg(res.message);
            if (res.status === 0) {
                window.parent.getUserInfo();
            }
        }
    })
}) 